//
//  Created by 邓盛明 on 2022/2/22.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit
@objc public enum ClassLive:Int {
     case small
     case large
     case live
 }
class TF_CourseDownLoadView: UIView {
    
    var titleLab:UILabel!
    @objc public var closeBtn:TalkfunIncreaseButton!
    @objc public var downLoadRoot:TF_CourseDownLoadRoot!
    @objc public var isShow:Bool = false
   
    @objc public var type:ClassLive = .small
    var lineView:UIView!
    var maskPath:UIBezierPath!
    var maskLayer:CAShapeLayer!
 
    let view = UIView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.isHidden = true
        
        self.addSubview(self.view)
        self.view.backgroundColor = .white

        self.titleLab = UILabel.init()
        self.titleLab.text = "课件下载";
        self.titleLab.font = UIFont.PingFang(size: 18)
        self.titleLab.textColor = UIColor(red: 20/255.0, green: 23/255.0, blue: 26/255.0, alpha: 1.0)
        self.titleLab.textAlignment = .center
        self.view.addSubview(self.titleLab)
        
        self.closeBtn = TalkfunIncreaseButton.init()
        self.closeBtn .setImage(UIImage.init(named: "HTLive.bundle/close_gray"), for: .normal)
        self.view.addSubview(self.closeBtn)
        
        self.downLoadRoot = TF_CourseDownLoadRoot.init()
        self.downLoadRoot.setCourseDownManagerdelegate()
        self.view.addSubview(self.downLoadRoot)
        
        self.lineView = UIView.init()
        self.lineView.backgroundColor = UIColor(red: 53/255.0, green: 56/255.0, blue: 59/255.0, alpha: 1.0)
        self.view.addSubview(self.lineView)
        

        self.closeBtn.addTarget(self, action:#selector(self.courseDownClosetBtnClick), for: .touchUpInside)
    }
    //课件下载关闭
   @objc func courseDownClosetBtnClick()
    {
        self.showCourseDownLoad(false)
    }

    @objc func showCourseDownLoad(_ isShow:Bool)
    {
        self.isShow = isShow
        self.showView(isShow)
    }
    
     func layoutSubviewsUI() {
    
        if self.type == .small {
            self.closeBtn.isHidden = true
            self.titleLab.textAlignment = .left
            self.titleLab.textColor = UIColor.white
            self.titleLab.frame = CGRect(x: 15, y: 10, width: self.view.frame.size.width-30, height: 25)
            self.lineView.isHidden = false
            self.downLoadRoot.frame = CGRect(x: 0, y: 46, width: self.view.frame.size.width, height: self.view.frame.size.height-46)
        } else {
            self.closeBtn.isHidden = false
            self.titleLab.textAlignment = .center;
            self.titleLab.textColor = UIColor(red: 20/255.0, green: 23/255.0, blue: 26/255.0, alpha: 1.0)
            self.titleLab.frame = CGRect(x: 15, y: 16, width: self.view.frame.size.width-30, height: 25)
            self.lineView.isHidden = true
            self.downLoadRoot.frame = CGRect(x: 0, y: 52, width: self.view.frame.size.width, height: self.view.frame.size.height-52)
        }
        self.closeBtn.frame = CGRect(x: self.view.frame.size.width-35, y: 16, width: 25, height: 25)
        self.lineView.frame = CGRect(x: 15, y: 45, width: self.view.frame.size.width-30, height: 1)
        self.downLoadRoot.type = self.type;
        self.downLoadRoot.isEdit = false
    }
    
    
    
    @objc public func initMask()  {
        let size = CGSize(width: 10, height: 10)
        if (self.maskPath == nil && self.bounds.size.width > 0) {
            self.maskPath = UIBezierPath.init(roundedRect: self.bounds, byRoundingCorners: [.topLeft,.topRight], cornerRadii: size)
            self.maskLayer = CAShapeLayer.init()
        }
        self.maskLayer.frame = self.bounds;
        self.maskLayer.path = self.maskPath.cgPath;
        self.layer.mask =  self.maskLayer;
    }
    
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func showView(_ isShow:Bool)
    {
        if (self.superview != nil) {
            self.superview?.addSubview(self)
        }
        //横屏模式
        if self.frame.width > self.frame.height {
            
            self.backgroundColor = UIColor.clear
            
            let viewW:CGFloat = 375
            var viewX = self.frame.width
            
            if isShow {
                viewX = self.frame.width - viewW
            }
            
            if isShow {
                self.view.frame = CGRect(x: self.frame.width, y: 0, width: viewW, height: self.frame.height)
               
            }
            self.layoutSubviewsUI()
          
            self.isHidden = false
            UIView.animate(withDuration: 0.5, delay: 0.0, options: UIView.AnimationOptions.allowAnimatedContent) {
                self.view.frame = CGRect(x: viewX, y: 0, width: viewW, height: self.frame.height)
            } completion: { Bool in
                
                if isShow == false {
                    self.isHidden = true
                }
            }
        }else
        {
            
            self.backgroundColor = UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 0.5)

            
            let viewH:CGFloat = 428
            var viewY = self.frame.height
            
            if isShow {
                viewY = self.frame.height - viewH
            }
            
            if isShow {
                self.view.frame = CGRect(x: 0, y: self.frame.height, width: self.frame.width, height: viewH)
               
            }
            
            self.layoutSubviewsUI()
            self.isHidden = false
            UIView.animate(withDuration: 0.5, delay: 0.0, options: UIView.AnimationOptions.allowAnimatedContent) {
                self.view.frame = CGRect(x: 0, y: viewY, width: self.frame.width, height: viewH)
            } completion: { Bool in
                
                if isShow == false {
                    self.isHidden = true
                }
            }
            
        }
        
    
        
    }
    
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.showView(false)
    }
    
    
//    override var frame: CGRect
//    {
//        didSet{
//        
//            if(self.isShow)
//            {
//                self.isShow = false
//                self.isHidden = true
//                
//            }
//            
//        }
//        
//        
//    }
//   
}
